package com.suzhe.ymall.common.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.suzhe.ymall.common.handler.AuthorizationHander;
import com.suzhe.ymall.common.handler.impl.AppAuthorizationHander;
import com.suzhe.ymall.common.handler.impl.ManageAuthorizationHander;
import com.suzhe.ymall.common.interceptor.AuthorizationInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Configuration
@Import({SwaggerConfig.class})
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Value("${permission.enabled:true}")
    private boolean permissionEnable;

    @Bean
    @ConditionalOnExpression("${permission.enabled:true}")
    public AuthorizationInterceptor authorizationInterceptor(){
        return new AuthorizationInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        if (permissionEnable){
            registry.addInterceptor(authorizationInterceptor())
                    .addPathPatterns("/**")//
                    .excludePathPatterns("/swagger-ui.html")//
                    .excludePathPatterns("/swagger-resources/**")//
                    .excludePathPatterns("/error")//
                    .excludePathPatterns("/webjars/**");;
        }
        super.addInterceptors(registry);
    }

    @ConditionalOnExpression("${permission.enabled:true}")
    @Bean("APP")
    public AuthorizationHander appAuthorizationHander(){
        return new AppAuthorizationHander();
    }

    @ConditionalOnExpression("${permission.enabled:true}")
    @Bean("MANAGE")
    public ManageAuthorizationHander manageAuthorizationHander(){
        return new ManageAuthorizationHander();
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

    /**
     * 避免long类型精度丢失
     * @param converters
     */
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
//        ObjectMapper objectMapper = new ObjectMapper();
//        /**
//         * 序列换成json时,将所有的long变成string
//         * 因为js中得数字类型不能包含所有的java long值
//         */
//        SimpleModule simpleModule = new SimpleModule();
//        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
//        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
//        objectMapper.registerModule(simpleModule);
//        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
//        converters.add(jackson2HttpMessageConverter);
//    }

}
